<?php
abstract class Veneer_Db_Table extends Zend_Db_Table_Abstract
{
	/*
	Configure the automatic actions through parameters.  Need to make it more errorproof

	protected $_name = 'TableName';
	protected $_primary = "PrimaryKeyFieldName or an array of File names";
	protected $_sequence = true;  //True or false - Does this table have an autoincrementing key
	protected $create_mysql = "SQL to create table for Mysql";
	*/
/*
	try to pass record count along
*/
	public $page_size = 20;
	public $record_count = 0;
	public $page_count = 0;
	public $table_info;

	protected $_registry;	// login
	protected $_dbAdapter;
	protected $_veneer_parms;	// array of parms
	protected $_veneer_log;	// array of parms
	protected $_site_config;
	protected $_msite_config;
	protected $_debug;	// login
	protected $_site_id;	// login



	function __construct()
	{
		parent::__construct();
		$this->_registry = Zend_Registry::getInstance();
		$this->_msite_config = $this->_registry->get('msite_config');
		$this->_site_config = $this->_registry->get('site_config');
		$this->_dbAdapter = $this->_registry->get('dbAdapter');
		$this->_veneer_log = $this->_registry->get('veneerLog');
		$this->_debug = $this->_registry->get('veneer_debug');
		$this->_site_id = $this->_msite_config->main->siteId;
	}
	public function insert( array $data)
	{
		unset($data['tags']);
		unset($data['addButton']);


		if($this->fieldInTable('dt_created')) {
			if(empty($data['dt_created'])) {
				$data['dt_created'] = date("Y-m-d H:m:s", time());
			}

		}
		$data['site_id'] = $this->_site_id;
/*
		if($this->fieldInTable('user_created')) {
			if(empty($data['user_created']))
				$data['user_created'] = time();
		}
*/
		return parent::insert($data);
	}

	public function update( array $data, $where)
	{
		unset($data['tags']);
		unset($data['addButton']);

		if($this->fieldInTable('dt_modified')) {
			if(empty($data['dt_modified']))
				$data['dt_modified'] = date("Y-m-d H:m:s", time());
		}
		$data['site_id'] = $this->_site_id;
		return parent::update($data, $where);
	}


	/*
	FetchUrlTitle - load entry based on id or url title
	*/
	public function fetchUrlToken( $url_title)
	{
		$where  = $this->getAdapter()->quoteInto('url_token = ?', $url_title);

		return $this->fetchRow($where);
	}
	protected function fieldInTable( $fieldName=none)
	{
		if(!isset($table_info['name'])) {
			$this->table_info = $this->info();
		}
		if(isset($this->table_info['metadata'][$fieldName]))
			return true;
		else
			return false;
	}

	public function test()
	{
		$info = $this->info();
		return $info['metadata']['dt_modified'];

	}
	public function fetchPage($pg=1,$where=null, $order=null)
	{
		$select = $this->_dbAdapter->select()->from(array('p' => $this->_name), array('cnt' => 'count(*)'));
		$stmt = $this->_dbAdapter->query($select);
		$result = $stmt->fetch();
		$this->record_count = $result['cnt'];
		$this->page_count = $this->record_count / $this->page_size;
		return $this->fetchAll($where, $order, $this->page_size, ($pg - 1 ) * $this->page_size );
		/*
			Add flag for pages before, pages after.
		*/
	}
	public function fetchObject($objectKeyField,$where=array())
	{
		// convert a series of object rows into a single keyed object
	}
}